package com.ant.androiddemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.core.content.ContextCompat;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ant.androiddemo.constant.Global;
import com.ant.androiddemo.util.ImageUtil;

public class EditPersonalActivity extends AppCompatActivity {

    private static final String TAG = Global.LOG_TAG;
    private static final String SPF_TAG = Global.SPF_TAG;
    private EditText etNickname, etAccount, etSchool, etSign;
    private TextView tvBirthday;
    private RadioButton rbBoy, rbGirl;
    private AppCompatSpinner spinnerCity;
    private ImageView ivAvatar;

    private String[] cities;
    private int selectedCityPosition;
    private String selectedCity;

    private String birthday;
    private String birthdayTime;

    private Uri imageUri;
    private String imageBase64;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_personal);
        initView();
        initData();
        initEvent();
    }

    private void initView(){

        etAccount = findViewById(R.id.et_account);
        etNickname = findViewById(R.id.et_nickname);
        rbBoy = findViewById(R.id.rb_boy);
        rbGirl = findViewById(R.id.rb_girl);
        tvBirthday = findViewById(R.id.tv_birthday_text);
        spinnerCity = findViewById(R.id.sp_city);
        etSchool = findViewById(R.id.et_school);
        etSign = findViewById(R.id.et_sign);

        ivAvatar = findViewById(R.id.iv_avatar);

    }
    private void initData(){
        cities = getResources().getStringArray(R.array.cities);
        SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
        String account = spf.getString("account", "");
        String nickname = spf.getString("nickname", "");
        String gender = spf.getString("gender", "");
        String birthday = spf.getString("birthday", "");
        String city = spf.getString("city", "");
        String school = spf.getString("school", "");
        String sign = spf.getString("sign", "");
        String image64 = spf.getString("image64", "");

        etAccount.setText(account);
        etNickname.setText(nickname);
        if (TextUtils.equals("男", gender)) {
            rbBoy.setChecked(true);
        }
        if (TextUtils.equals("女", gender)) {
            rbGirl.setChecked(true);
        }
        tvBirthday.setText(birthday);
        for (int i = 0; i < cities.length; i++) {
            if(TextUtils.equals(cities[i], city)){
                selectedCityPosition = i;
                break;
            }
        }
        spinnerCity.setSelection(selectedCityPosition);
        etSchool.setText(school);
        etSign.setText(sign);
        ivAvatar.setImageBitmap(ImageUtil.base64ToImage(image64));
    }



    private void initEvent(){
        spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                selectedCityPosition =position;
                selectedCity = cities[position];
                Log.d(TAG, "onItemSelected: --->" + position);
                Log.d(TAG, "onItemSelected: --->" + selectedCity);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        tvBirthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //跳出日期
                new DatePickerDialog(EditPersonalActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        // 处理用户选择的日期
                        // year: 年份
                        // month: 月份 (0-11)
                        // dayOfMonth: 日
                        int realMonth = month +1;
                        birthday = year + "年" + realMonth + "月" + dayOfMonth + "日";
                        Log.d(TAG, "onDateSet: ---->" + birthday);
                        popTimePick();
                    }
                }, 2000, 10, 23).show();
            }
        });
    }

    //跳出小时
    private void popTimePick(){
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                birthdayTime = birthday + hourOfDay+"时"+minute+"分";
                Log.d(TAG, "onTimeSet: --->" + birthdayTime);
                tvBirthday.setText(birthdayTime);
            }
        }, 12, 36, true).show();
    }



    public void save(View view) {

        String account = etAccount.getText().toString();
        String nickname = etNickname.getText().toString();
        String school = etSchool.getText().toString();
        String sign = etSign.getText().toString();
        String gender = "男";
        if(rbGirl.isChecked()){
            gender = "女";
        }

        SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();

        editor.putString("account", account);
        editor.putString("nickname", nickname);
        editor.putString("gender", gender);
        editor.putString("birthday", birthdayTime);
        editor.putString("city", selectedCity);
        editor.putString("school", school);
        editor.putString("sign", sign);
        editor.putString("image64", imageBase64);

        editor.apply();
        Intent intent = new Intent(this, UserActivity.class);
        startActivity(intent);
    }

    public void takePhoto(View view) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //指定拍照
        startActivity(intent);
    }
}